﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucQuanLyUser : UserControl
    {
        public ucQuanLyUser()
        {
            InitializeComponent();
            LoadDataKhoa();
            LoadNhanVien();
            LoadData();
            LoadGrid();
            this.cmKhoa.SelectedIndexChanged += new System.EventHandler(this.cmKhoa_SelectedIndexChanged);
            button2.Visible = false;
            button3.Visible = false;
        }

        void LoadDataKhoa()
        {
            KhoaBUO khoaobj = new KhoaBUO();
            cmKhoa.DataSource = khoaobj.GetAll();
            cmKhoa.DisplayMember = "WardName";
            cmKhoa.ValueMember = "id_Ward";
        }
        void LoadNhanVien()
        {
            NhanVienBUO nv = new NhanVienBUO();
            if (nv.GetAllNhanVienByKhoa(int.Parse(cmKhoa.SelectedValue.ToString())).Select(n => new { TenNV = n.FirstName + " " + n.LastName, ID = n.NhanVienID }).ToList().Count == 0)
                cbNhanVien.Text = "";
            cbNhanVien.DataSource = nv.GetAllNhanVienByKhoa(int.Parse(cmKhoa.SelectedValue.ToString())).Select(n => new { TenNV=n.FirstName+" "+ n.LastName,ID=n.NhanVienID}).ToList();
            cbNhanVien.DisplayMember = "TenNV";
            cbNhanVien.ValueMember = "ID";

        }

        private void cmKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadNhanVien();
        }
        void LoadData()
        {
            ComboboxItem item1 = new ComboboxItem("Giám đốc", (int)PhanQuyen.GiamDoc);
            ComboboxItem item2 = new ComboboxItem("Quản lý nhân sự", (int)PhanQuyen.QuanLyNhanSu);
            ComboboxItem item3 = new ComboboxItem("Charge Nurse",(int)PhanQuyen.ChargeNures );

            List<ComboboxItem> list = new List<ComboboxItem>();
            list.Add(item1);
            list.Add(item2);
            list.Add(item3);

            cbbQuyen.DataSource = list;
            cbbQuyen.ValueMember = "Value";
            cbbQuyen.DisplayMember = "Text";


        }

        private void button1_Click(object sender, EventArgs e)
        {

            BoNhiemBUO bn = new BoNhiemBUO();
            UserBUO userbo = new UserBUO();
            tbl_User user = new tbl_User();
            user.NhanVienID = int.Parse(cbNhanVien.SelectedValue.ToString());
            //  MessageBox.Show(cbbQuyen.SelectedValue.ToString());
            user.Id_Quyen = int.Parse(cbbQuyen.SelectedValue.ToString());

            if (userbo.checkUserExits(txtUserName.Text))
            {
                MessageBox.Show("Username đã tồn tại");
                return;
            }
            else
            {
                user.UserName = txtUserName.Text;
                user.Password = MaHoa.EncryptMD5(txtPass.Text);
            }
            try
            {
                if (bn.Insert(user))
                {

                    MessageBox.Show("Thành công");
                }
                else
                {

                    MessageBox.Show("Thất bại");
                }


            }
            catch (Exception)
            {

                throw;
            }

            LoadGrid();
        }
        void LoadGrid()
        {
            UserBUO userbuo = new UserBUO();
            NhanVienBUO nh = new NhanVienBUO();
            dataGridView1.DataSource = userbuo.GetAll();
           // dataGridView1.DataSource = userbuo.GetAll();
           
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.Columns[2].Visible=false;
          //  dataGridView1.Columns[4].Visible=false;
         //   dataGridView1.Columns[5].Visible=false;
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null;

            }
        }

        String ID_Co;

        private void xemChiTieeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BoNhiemBUO bn = new BoNhiemBUO();
            NhanVienBUO nv = new NhanVienBUO();
            tbl_User obj =  bn.GetSingle(int.Parse(ID_Co));
            txtUserName.Text = obj.UserName;
            if (obj.NhanVienID != null)
            {
                cmKhoa.SelectedValue = nv.GetMaKhoa(int.Parse(obj.NhanVienID.Value.ToString()));
               
                LoadNhanVien();
                cbNhanVien.SelectedValue = obj.NhanVienID;
            }
            else
            {
                cmKhoa.Text = "";
                cbNhanVien.Text = "";
            }
            cbbQuyen.SelectedValue = obj.Id_Quyen;



        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserBUO user = new UserBUO();
            if(user.Delete(int.Parse(ID_Co)))
            {
                MessageBox.Show("Xóa thành công");
            }
            else
            {
                MessageBox.Show("Xóa thất bại");
            }

            LoadGrid();
            
            
        }

        private void thayĐổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BoNhiemBUO bn = new BoNhiemBUO();
            NhanVienBUO nv = new NhanVienBUO();
            tbl_User obj = bn.GetSingle(int.Parse(ID_Co));
            txtUserName.Text = obj.UserName;
            if (obj.NhanVienID != null)
            {
                cmKhoa.SelectedValue = nv.GetMaKhoa(int.Parse(obj.NhanVienID.Value.ToString()));

                LoadNhanVien();
                cbNhanVien.SelectedValue = obj.NhanVienID;
                txtPass.Text = "Nhập mật khẩu mới";
            }
            else
            {
                cmKhoa.Text = "";
                cbNhanVien.Text = "";
            }
            cbbQuyen.SelectedValue = obj.Id_Quyen;
            button2.Visible = true;
            button3.Visible = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            BoNhiemBUO bn = new BoNhiemBUO();
            UserBUO userbo = new UserBUO();
            tbl_User user = new tbl_User();
            
            //  MessageBox.Show(cbbQuyen.SelectedValue.ToString());
            

          
             
                user.Password = MaHoa.EncryptMD5(txtPass.Text);
            
            try
            {
                if (userbo.Update(int.Parse(ID_Co),user))
                {

                    MessageBox.Show("Thành công");
                    button2.Visible = false;
                }
                else
                {

                    MessageBox.Show("Thất bại");
                    button2.Visible = true;
                }


            }
            catch (Exception)
            {

                throw;
            }

            LoadGrid();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            BoNhiemBUO bn = new BoNhiemBUO();
            UserBUO userbo = new UserBUO();
            tbl_User user = new tbl_User();
            user.NhanVienID = int.Parse(ID_Co);
            //  MessageBox.Show(cbbQuyen.SelectedValue.ToString());
            user.Id_Quyen = int.Parse(cbbQuyen.SelectedValue.ToString());


            user.UserName = txtUserName.Text;
            //user.Password = MaHoa.EncryptMD5(txtPass.Text);

            try
            {
                if (userbo.Update(int.Parse(ID_Co), user,true))
                {
                    MessageBox.Show("Thành công");
                    button3.Visible = false;
                    

                }
                else
                {

                    MessageBox.Show("Thất bại");
                    button3.Visible = true;
                }


            }
            catch (Exception)
            {

                throw;
            }
            LoadGrid();
            
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BoNhiemBUO bn = new BoNhiemBUO();
            NhanVienBUO nv = new NhanVienBUO();
            tbl_User obj = bn.GetSingle(int.Parse(ID_Co));
            txtUserName.Text = obj.UserName;
            if (obj.NhanVienID != null)
            {
                cmKhoa.SelectedValue = nv.GetMaKhoa(int.Parse(obj.NhanVienID.Value.ToString()));

                LoadNhanVien();
                cbNhanVien.SelectedValue = obj.NhanVienID;
            }
            else
            {
                cmKhoa.Text = "";
                cbNhanVien.Text = "";
            }
            cbbQuyen.SelectedValue = obj.Id_Quyen;

            button3.Visible = true;
            button2.Visible = false;

        }
    }
}
